package com.hospital.hospitalserver.service;

import com.hospital.hospitalserver.entity.Vo.PatientMaster;
import com.hospital.hospitalserver.entity.query.DoctorDiagnosisRecordAndPatientMasterQuery;
import com.hospital.hospitalserver.entity.query.DoctorPatientMasterQuery;
import com.hospital.hospitalserver.mapper.PatientMasterMapper;
import com.hospital.hospitalserver.util.PagerData;
import com.hospital.hospitalserver.util.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 患者档案信息管理实现类
 * */
@Service
@RequiredArgsConstructor
public class PatientMasterService {
    private final PatientMasterMapper patientMasterMapper;

    /**
     * 医生工作台端
     * 加载患者档案数据分页数据总数
     * */
    private Integer getPatientMasterDataCountByDoctorId(DoctorPatientMasterQuery query){
        return patientMasterMapper.getPatientMasterDataCountByDoctorId(query);
    }

    /**
     * 计算分页
     * */
    private Integer getPatientMasterDataTotolPagesByDoctorId(DoctorPatientMasterQuery query){
        Integer patientMasterDataCount = getPatientMasterDataCountByDoctorId(query);
        Integer pageTotal = 1;//设置初始页码为1
        //数据总条数除以pageSize每页显示条数，如取余数求模，如果有余数，页码+1
        //如果可以整除，则页码等于数据总数除以pageSize
        pageTotal = patientMasterDataCount/query.getPageSize();
        if(patientMasterDataCount%query.getPageSize()>0){
            pageTotal += 1;
        }
        return pageTotal;
    }


    /**
     * 医生工作台端
     * 加载患者档案数据分页
     * */
    private List<PatientMaster> getPatientMasterMapperDataByDoctorId(DoctorPatientMasterQuery query){
        Integer currentPage = query.getCurrentPage();
        Integer startIndex = (currentPage - 1)*query.getPageSize();
        query.setStartIndex(startIndex);
        return patientMasterMapper.getPatientMasterMapperDataByDoctorId(query);
    }

    /**
     * 医生工作台端
     * 装载患者档案数据分页
     * */
    @Transactional
    public Response getPatientMasterPagerDataByDoctorSearch(DoctorPatientMasterQuery query){
        PagerData pagerData = new PagerData();
        pagerData.setTotal(getPatientMasterDataCountByDoctorId(query));
        pagerData.setPageTotal(getPatientMasterDataTotolPagesByDoctorId(query));
        pagerData.setData(getPatientMasterMapperDataByDoctorId(query));
        return Response.success(pagerData);
    }

    /**
     * 医生工作台
     * 根据患者档案id查询患者基础信息
     * */
    public Response getPatientMasterAndRecordListDataById(DoctorDiagnosisRecordAndPatientMasterQuery query){
        return Response.success(patientMasterMapper.getPatientMasterAndRecordListDataById(query));
    }
}
